package com.fastloan.mgt.model.controller;

import com.fastloan.mgt.model.model.ApplicationModel;
import com.fastloan.mgt.model.model.vo.TobaccoModelVo;
import com.fastloan.mgt.model.service.ModelCalculateService;
import com.kawins.result.ResultTo;
import com.kawins.spring.valid.core.BeanValid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import static com.fastloan.mgt.model.service.impl.ModelCalculateServiceImpl.NO_PASS;
import static com.fastloan.mgt.model.service.impl.ModelCalculateServiceImpl.PASS;

/**
 * @author yanwe
 * createTime 2017-12-2017/12/14 15:17
 */
@RestController
@RequestMapping("/api/model")
public class ModelController {

    @Autowired
    private ModelCalculateService modelCalculateService;

    @BeanValid
    @RequestMapping("/anon/tobacco/bbdEvaluateCallback")
    public ResultTo tobaccoCreditModel(@Valid @RequestBody TobaccoModelVo tobaccoModelVo, BindingResult bindingResult){
        ApplicationModel applicationModel = new ApplicationModel();
        applicationModel.setApplicationId(tobaccoModelVo.getSerialNumber());
        applicationModel.setPass(tobaccoModelVo.isPassed() ? PASS : NO_PASS);
        applicationModel.setModelResult(tobaccoModelVo.getCreditLine());
        applicationModel.setInterestRateResult(tobaccoModelVo.getInterestRate());
        applicationModel.setRatingLevel(tobaccoModelVo.getRatingLevel());
        applicationModel.setSuggestion(tobaccoModelVo.getSuggestion());
        modelCalculateService.saveModelResult(applicationModel);
        return new ResultTo();
    }
}
